// pages/guess/guess.js
const { songs } = require('../../data/songs.js');

Page({
  data: {
    currentSong: null,
    currentTime: '00:00',
    isPlaying: false,
    popularSongs: [],
    playlists: [
      { id: 1, name: '午后花园', cover: '/assets/playlists/1.png' },
      { id: 2, name: '深夜咖啡', cover: '/assets/playlists/2.png' },
      { id: 3, name: '雨天爵士', cover: '/assets/playlists/3.png' }
    ],
    playMode: 'loop', // 'loop', 'single', 'random'
    progress: 0, // 播放进度（0-100）
    isSeeking: false, // 是否正在拖动进度条
  },

  onLoad() {
    // 初始化数据
    const app = getApp();
    const playerManager = app.globalData.playerManager;
    
    this.setData({
      currentSong: playerManager.currentSong || songs[0],
      isPlaying: playerManager.isPlaying,
      popularSongs: songs.filter(s => s.id !== (playerManager.currentSong || songs[0]).id)
    });

    // 监听播放状态变化
    playerManager.watchPlayStatus(this.updatePlayStatus.bind(this));

    // 监听播放时间更新
    playerManager.audioManager.onTimeUpdate(() => {
      if (this.data.isSeeking) return;
      const progress = (playerManager.audioManager.currentTime / playerManager.audioManager.duration) * 100;
      const currentTime = this.formatTime(playerManager.audioManager.currentTime);
      this.setData({ progress, currentTime });
    });
  },

  onUnload() {
    // 页面卸载时移除监听
    const app = getApp();
    app.globalData.playerManager.unwatchPlayStatus(this.updatePlayStatus.bind(this));
  },

  // 更新播放状态
  updatePlayStatus(globalData) {
    this.setData({
      currentSong: globalData.currentSong,
      isPlaying: globalData.isPlaying,
      popularSongs: songs.filter(s => s.id !== globalData.currentSong?.id)
    });
  },

  // 播放/暂停
  onPlayTap() {
    getApp().globalData.playerManager.togglePlay();
  },

  // 上一首
  onPrevTap() {
    const playerManager = getApp().globalData.playerManager;
    playerManager.playPrev();
  },

  // 下一首
  onNextTap() {
    const playerManager = getApp().globalData.playerManager;
    playerManager.playNext();
  },

  // 点击播放列表
  onPlaylistTap() {
    wx.navigateTo({
      url: '/pages/playlist/playlist'
    });
  },

  // 点击歌曲列表中的歌曲
  onSongTap(e) {
    const songId = e.currentTarget.dataset.songId;
    const song = songs.find(s => s.id === songId);
    if (song) {
      getApp().globalData.playerManager.playSong(song);
    }
  },

  // 收藏/取消收藏
  onLikeTap() {
    const { currentSong } = this.data;
    currentSong.isLiked = !currentSong.isLiked;
    this.setData({ currentSong });
  },

  // 进度条拖动开始
  onSliderDragStart() {
    this.setData({ isSeeking: true });
  },

  // 进度条拖动中
  onSliderChange(e) {
    const playerManager = getApp().globalData.playerManager;
    const progress = e.detail.value;
    const currentTime = (progress / 100) * playerManager.audioManager.duration;
    playerManager.audioManager.seek(currentTime);
    this.setData({
      progress,
      currentTime: this.formatTime(currentTime),
      isSeeking: false
    });
  },

  // 返回上一页
  onBackTap() {
    wx.navigateBack();
  },

  // 格式化时间
  formatTime(seconds) {
    const minutes = Math.floor(seconds / 60);
    seconds = Math.floor(seconds % 60);
    return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  }
});